package com.idea.zd.sys.util;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class createWord {

    private String filePath; //文件路径
    private String fileName; //文件名称
    private String fileOnlyName; //文件唯一名称

    public String createWord() {
        /** 用于组装word页面需要的数据 */
        Map<String, Object> dataMap = new HashMap<String, Object>();

        /** 组装数据 */

        dataMap.put("main_class","杨贞泽");

        /** 文件名称，唯一字符串 */
        Random r=new Random();
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
        StringBuffer sb=new StringBuffer();
        sb.append(sdf1.format(new Date()));
        sb.append("_");
        sb.append(r.nextInt(100));

        //文件路径
        filePath="upload";

        //文件唯一名称
        fileOnlyName = "用freemarker导出的Word文档_"+sb+".doc";

        //文件名称
        fileName="用freemarker导出的Word文档.doc";

        /** 生成word */
        WordUtil.createWord(dataMap, "test.ftl", filePath, fileOnlyName);

        return "createWordSuccess";
    }

    public static void main(String [] args) {

        /*Map<String ,String> map = new HashMap<>();
        map.put("1","2");

        System.out.println(map.get("3"));*/

        int[][] arr1 = {{1,2}, {2, 3}, {4, 5}};
        System.out.println("arr1的数值：");
        for(int i=0; i<3; ++i)
            for(int j=0; j<2; ++j)
                System.out.println(arr1[i][j]);


        /*String uri = "http://www.sucaitianxia.com/sheji/pic/200902/20090208015345473123.jpg";

        System.out.println(uri.startsWith("http"));

        try{
            URL url = new URL(uri);
            InputStream inputStream =  url.openStream();

            DataInputStream dataInputStream = new DataInputStream(inputStream);
            //File file = File.createTempFile("123213",uri.substring(uri.lastIndexOf(".")-1,uri.length()));

            String imgPath = "/Users/yangzhenze/Downloads/"+uri.substring(uri.lastIndexOf("/"),uri.length());


            FileOutputStream fileOutputStream = new FileOutputStream(imgPath);
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }*/

    }

    /*public static void main(String[] args) {
        String path = System.getProperty("user.dir")+"/src/main/resources/template/";
        Map<String,Object> map = new HashMap<>();
        map.put("${123}","321");
        XWPFDocument document = new XWPFDocument();
        XWPFTable table = document.createTable();
        //表格创建
        XWPFTableRow tableRowTitle = table.getRow(0);
        tableRowTitle.getCell(0).setText("标题");
        tableRowTitle.addNewTableCell().setText("内容");
        tableRowTitle.addNewTableCell().setText("姓名");
        tableRowTitle.addNewTableCell().setText("日期");
        tableRowTitle.addNewTableCell().setText("备注");
        map.put("table",table);





        WordUtilPoi.searchAndReplace(path+"地面值班日志21.docx",path+"地面值班日志12.docx",map);
    }*/
}
